<template>
  <div class="container">
    <el-form
      :inline="true"
      :model="pageParam"
      style="text-align: center"
      class="demo-form-inline"
    >
      <el-form-item label="课程标题">
        <el-input v-model="pageParam.title" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item label="课程发布情况">
        <el-select v-model="pageParam.status">
          <el-option label="默认全选" :value="null"></el-option>
          <el-option label="发布" value="Normal"></el-option>
          <el-option label="未发布" value="Draft"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-share"
          @click="getCourseBasicInfoList()"
          >查询</el-button
        >
        <el-button type="primary" icon="el-icon-delete" @click="resetData()"
          >清空</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      :row-style="{ height: '120px' }"
      :data="list.rows"
      border
      style="width: 100%"
    >
      <el-table-column type="index" label="编号" width="180"> </el-table-column>
      <el-table-column
        prop="title"
        label="课程名称"
        width="250"
      ></el-table-column>
      <el-table-column prop="lessonNum" label="总课时" width="180">
        <template slot-scope="scope">
          <el-tag type="success" effect="dark" size="medium"
            ><span style="font-size: 18px"> {{ scope.row.lessonNum }}课时</span>
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="课程售价" width="180">
        <template slot-scope="scope">
          <el-tag type="success" effect="dark" size="medium"
            ><span style="font-size: 18px"> {{ scope.row.price }}元</span>
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="发布状态" width="180">
        <template slot-scope="scope">
          {{ scope.row.status === "Normal" ? "已发布" : "未发布" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="viewCount"
        label="课程浏览量"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="buyCount"
        label="课程购买量"
        width="180"
      ></el-table-column>

      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <router-link :to="'/course/info/' + scope.row.id">
            <el-button
              icon="el-icon-edit"
              type="primary"
              plain
              @click="updateCourseInfo"
              >修改课程内容</el-button
            >
          </router-link>
          <el-button
            icon="el-icon-delete"
            type="danger"
            plain
            @click="removeById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size="list.pageSize"
      :pager-count="list.current"
      :total="list.total"
      background
      style="padding: 30px 0; text-align: center"
      layout="total ,prev, pager, next ,jumper"
      @current-change="getCourseBasicInfoList"
    >
    </el-pagination>
  </div>
</template> 

<script>
import courseApi from "@/api/edu/course";

export default {
  name: "TeacherList",
  data() {
    return {
      list: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        rows: [],
      },
      pageParam: {
        page: "1",
        pageSize: "10",
        title: "", //课程标题
        status: "", //课程发布情况
      },
    };
  },
  created() {
    this.getCourseBasicInfoList();
  },
  methods: {
    //修改表单信息
    updateCourseInfo() {
      console.log("111");
    },

    //清空表单基本信息
    resetData() {
      this.pageParam.title = "";
      this.pageParam.status = "";
    },

    //获取表单基本数据
    getCourseBasicInfoList(page = 1) {
      this.pageParam.page = page;
      courseApi.getCourseBasicInfo(this.pageParam).then((response) => {
        // console.log(response.data);
        this.list.total = response.data.total;
        this.list.pageSize = response.data.pageSize;
        this.list.currentPage = response.data.currentPage;
        this.list.rows = response.data.courseBasicInfo;
      });
    },

    //删除此课程
    removeById(courseId) {
      this.$confirm("此操作讲删除该课程所有信息，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        /**
         * 1. 如果点击确认 则进行删除
         * 2. 弹出删除成功的消息
         * 3. 刷新页面
         */
        courseApi.deleteCourse(courseId).then((response) => {
          this.$message({
            type: "success",
            message: "删除课程成功",
          });

          this.getCourseBasicInfoList();
        });
      });
    },
  },
};
</script>

<style >
</style>